﻿<Page x:Class="MaxAlarm.Pages.SettingsPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:ViewModels="clr-namespace:MaxAlarm.ViewModels" xmlns:Pages="clr-namespace:MaxAlarm.Pages" mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
	  Title="SettingsPage"
      Pages:SettingsPage.ViewModel="{DynamicResource SettingsViewModel}">

    <Page.Resources>
        <ViewModels:SettingsPageViewModel x:Key="SettingsViewModel"
                                          PageManager="{StaticResource PagesManager}"/>
    </Page.Resources>

    <Grid DataContext="{Binding Path=ViewModel, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Pages:SettingsPage}}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TabControl Margin="10"
                    Background="{StaticResource DefaultBackgroundBrush}">
            
            <TabItem Header="Custom">
                <Grid Margin="10" HorizontalAlignment="Stretch"
                      DataContext="{Binding Path=Settings}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <TextBlock Style="{StaticResource BigWhite}"
                               Text="Base address:"
                               Grid.Row="0"
                               Grid.Column="0"
                               Margin="5"
                               VerticalAlignment="Center"
                               HorizontalAlignment="Right"/>
                    
                    <TextBox Style="{StaticResource LoginBox}"
                             Text="{Binding Path=BaseAddress}"
                             Grid.Row="0"
                             Grid.Column="1"
                             Margin="5"
                             VerticalAlignment="Center"
                             HorizontalAlignment="Left"
                             Width="200"/>

                    
                    
                    <TextBlock Style="{StaticResource BigWhite}"
                               Text="Autorefresh interval:"
                               Grid.Row="1"
                               Grid.Column="0"
                               Margin="5"
                               VerticalAlignment="Center"
                               HorizontalAlignment="Right"/>

                    <TextBox Style="{StaticResource LoginBox}"
                             Text="{Binding Path=AutoRefreshInterval}"
                             Grid.Row="1"
                             Grid.Column="1"
                             Margin="5"
                             VerticalAlignment="Center"
                             HorizontalAlignment="Left"
                             Width="200"/>

                    
                    
                    
                    <TextBlock Style="{StaticResource BigWhite}"
                               Text="Startup filters:"
                               Grid.Row="2"
                               Grid.Column="0"
                               Margin="5"
                               VerticalAlignment="Center"
                               HorizontalAlignment="Right"/>

                    <TextBox Style="{StaticResource LoginBox}"
                             Text="{Binding Path=StartupFilters}"
                             Grid.Row="2"
                             Grid.Column="1"
                             Margin="5"
                             VerticalAlignment="Center"
                             HorizontalAlignment="Left"
                             Width="200"/>


                    <Separator Grid.Row="3"
                               Grid.ColumnSpan="2"
                               VerticalAlignment="Bottom"
                               Margin="10"/>
                    
                    <TextBlock Style="{StaticResource BigWhite}"
                               Text="Play sound:"
                               Grid.Row="4"
                               Grid.Column="0"
                               Margin="5"
                               VerticalAlignment="Center"
                               HorizontalAlignment="Right"/>

                    <CheckBox IsChecked="{Binding Path=AlertSettings.PlaySound}"
                             Grid.Row="4"
                             Grid.Column="1"
                             Margin="5"
                             VerticalAlignment="Center"
                             HorizontalAlignment="Left"
                              x:Name="NeedPlaySound"/>

                    <TextBlock Style="{StaticResource BigWhite}"
                               Text="Play cyclic:"
                               Grid.Row="5"
                               Grid.Column="0"
                               Margin="5"
                               VerticalAlignment="Center"
                               HorizontalAlignment="Right"/>

                    <CheckBox IsChecked="{Binding Path=AlertSettings.IsCyclic}"
                             Grid.Row="5"
                             Grid.Column="1"
                             Margin="5"
                             VerticalAlignment="Center"
                             HorizontalAlignment="Left"
                              IsEnabled="{Binding Path=IsChecked, ElementName=NeedPlaySound}"/>


                    <TextBlock Style="{StaticResource BigWhite}"
                               Text="Path to sound file:"
                               Grid.Row="6"
                               Grid.Column="0"
                               Margin="5"
                               VerticalAlignment="Center"
                               HorizontalAlignment="Right"/>

                    <StackPanel Orientation="Horizontal"
                                Grid.Row="6"
                                Grid.Column="1"
                                Margin="5"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Left">
                        
                    
                        <TextBox Style="{StaticResource LoginBox}"
                                 Text="{Binding Path=AlertSettings.PathToFile}"
                                 Width="200"
                                 IsEnabled="{Binding Path=IsChecked, ElementName=NeedPlaySound}"
                                 IsReadOnly="True"
                                 x:Name="PathBox"/>

                        <Button Margin="10,0,0,0" 
                                Style="{StaticResource LoginButtons}"
                                Command="{x:Static ViewModels:SettingsPageViewModel.SelectFileCommand}"
                                MinWidth="50"
                                IsEnabled="{Binding Path=IsChecked, ElementName=NeedPlaySound}">
                            <Button.Content>
                                <TextBlock Text="..." 
                                           Style="{StaticResource BigWhite}"/>
                            </Button.Content>
                        </Button>

                    </StackPanel>


                    <Separator Grid.Row="7"
                               Grid.ColumnSpan="2"
                               VerticalAlignment="Bottom"
                               Margin="10"/>


                    <Button Margin="0,10,0,10" 
                            Style="{StaticResource LoginButtons}"
                            Command="{x:Static ViewModels:SettingsPageViewModel.ClearLoginsCommand}"
                            Width="100"
                            Grid.Column="1"
                            Grid.Row="8"
                            HorizontalAlignment="Left"
                            Height="30">
                        <Button.Content>
                            <TextBlock Text="Clear logins" 
                                       Style="{StaticResource BigWhite}"/>
                        </Button.Content>
                    </Button>


                </Grid>
            </TabItem>

            <TabItem Header="Alarms">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    
                    <Grid Grid.Row="0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <StackPanel Grid.Row="0"
                                    Margin="0,3,0,3"
                                    Orientation="Horizontal"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Top"
                                    Background="Transparent">

                            <ToggleButton VerticalAlignment="Center"
                                          HorizontalAlignment="Center"
                                          ToolTip="Add alarm descriptor"
                                          Margin="5,0,5,0"
                                          x:Name="AddButton"
                                          IsChecked="{Binding Path=IsAddPanelVisible, Mode=TwoWay}"
                                          Command="{x:Static ViewModels:SettingsPageViewModel.AddItemCommand}">
                                <Canvas Background="{StaticResource AddIconBrush}"
							            Width="24"
							            Height="24" />
                            </ToggleButton>

                            <ToggleButton VerticalAlignment="Center"
                                          HorizontalAlignment="Center"
                                          ToolTip="Add alarm descriptor"
                                          Margin="5,0,5,0"
                                          x:Name="EditButton"
                                          IsChecked="{Binding Path=IsEditPanelVisible, Mode=TwoWay}"
                                          Command="{x:Static ViewModels:SettingsPageViewModel.EditItemCommand}">
                                <Canvas Background="{StaticResource EditIconBrush}"
							            Width="24"
							            Height="24" />
                            </ToggleButton>

                            <Button Style="{StaticResource BottomButtonStyle}"
                                    VerticalAlignment="Center"
                                    HorizontalAlignment="Center"
                                    ToolTip="Remove alarm descriptor"
                                    Margin="5,0,5,0"
                                    Command="{x:Static ViewModels:SettingsPageViewModel.RemoveItemCommand}">
                                <Canvas Background="{StaticResource RemoveIconBrush}"
							            Width="24"
							            Height="24" />
                            </Button>
                        </StackPanel>

                        <Grid Grid.Row="1">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            
                            <TextBlock Text="{Binding Path=ErrorText}"
                                       Visibility="{Binding Path=HasError, Converter={StaticResource BooleanToVisibilityConverter}}"
                                       Style="{StaticResource BigWhite}"
                                       Foreground="LightCoral"
                                       VerticalAlignment="Center"
                                       HorizontalAlignment="Center"
                                       Margin="10"
                                       TextWrapping="Wrap" />
                            
                            <ContentControl ContentTemplate="{StaticResource AddAlarmPanelTemplate}" 
                                            Grid.Row="1"
                                            Visibility="{Binding Path=IsAddPanelVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
                                            Content="{Binding Path=TempItem}"/>

                            <ContentControl ContentTemplate="{StaticResource AddAlarmPanelROTemplate}" 
                                            Grid.Row="1"
                                            Visibility="{Binding Path=IsEditPanelVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
                                            Content="{Binding Path=TempItem}"/>

                        </Grid>

                        <Canvas Background="{StaticResource LineBrush}"
				                Height="1"
				                HorizontalAlignment="Stretch"
				                VerticalAlignment="Bottom"
				                Margin="2,0,2,0"
                                Grid.Row="1"/>

                    </Grid>
                    
                    <ListView Grid.Row="1" 
                              ItemsSource="{Binding Path=Settings.AlarmSettings.Items}"
                              Margin="5"
                              BorderThickness="0"
                              SelectedItem="{Binding Path=SelectedAlarm}"
                              ItemTemplate="{StaticResource AlarmListItem}"
                              ItemContainerStyle="{StaticResource AlarmItemContainerStyle}"
                              IsEnabled="{Binding Path=IsEditPanelVisible, Converter={StaticResource InvertBooleanConverter}}"
                              SelectionMode="Single">

                    </ListView>
                </Grid>
                
            </TabItem>
        </TabControl>

        <Canvas Background="{StaticResource LineBrush}"
				            Height="1"
				            HorizontalAlignment="Stretch"
				            VerticalAlignment="Bottom"
				            Margin="2,0,2,0"
                            Grid.Row="0"/>

        <Border Background="Transparent"
                Grid.Row="1">
            <Grid x:Name="BottomGrid" 
                  Background="Transparent">
                <StackPanel Grid.Row="2"
                            Margin="0,3,0,3"
                            Orientation="Horizontal"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Background="Transparent">

                    <Button Style="{StaticResource BottomButtonStyle}"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Center"
                            ToolTip="Save"
                            Margin="5,0,5,0"
                            Command="{x:Static ViewModels:SettingsPageViewModel.SaveCommand}">

                        <Canvas Background="{StaticResource SaveIconBrush}"
							    Width="32"
							    Height="32" />
                    </Button>

                    <Button Style="{StaticResource BottomButtonStyle}"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Center"
                                ToolTip="Cancel"
                            Margin="5,0,5,0"
                            Command="{x:Static ViewModels:SettingsPageViewModel.CancelCommand}">
                        <Canvas Background="{StaticResource CancelIconBrush}"
							        Width="32"
							        Height="32" />
                    </Button>

                </StackPanel>

            </Grid>
        </Border>
        
    </Grid>
</Page>
